package com.sxbbc.plat.controller.jz;

import java.util.List;

import com.sxbbc.common.basics.utils.statics.StaticUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.function.common.GetRest;
import com.function.common.RestResponse;
import com.sxbbc.common.basics.rest.RestFactory;
import com.sxbbc.common.core.entity.jz.Address;
import com.sxbbc.common.core.service.IAddressService;
import com.sxbbc.common.core.utils.BasePage;
import com.sxbbc.plat.common.MappingUtils;

/**
 * @author leehao
 * @description 地址管理
 * @date 2019-04-29 10:35
 */
@Controller
public class AddressController {
    @Autowired
    private IAddressService addressService;

    @RequestMapping(value = MappingUtils.SHOW_ADDRESS_LIST)
    public ModelAndView showAddressList(BasePage<Address> page, Address address) {
        address.setPid(StaticUtils.ADDRESS_COUNTRY);
        if (address.getProId() != null) {
            address.setPid(address.getProId());
        }
        List<Address> allList = addressService.queryAddressByParentId(address);
        ModelAndView view = new ModelAndView(MappingUtils.SHOW_ADDRESS_LIST);
        view.addObject("allList", allList);
        return view;
    }

    @RequestMapping(value = MappingUtils.QUERY_ADDRESS_LIST)
    @ResponseBody
    public com.sxbbc.common.basics.rest.RestResponse queryAddressByPid(Integer proId, Integer cityId, Integer areaId, 
    		Address address,BasePage<Address> page) {
    	Integer pid = null;
    	if (proId != null) {
    		pid = proId;
    	}
    	if (cityId != null) {
    		pid = cityId;
    	}
    	if (areaId != null) {
    		pid = areaId;
    	}
        address.setPid(pid);
        Page<Address> pageInfo = addressService.queryAddressPage(page,address);
        return RestFactory.success(pageInfo.getRecords(), pageInfo.getTotal());
    }

    @RequestMapping(value = MappingUtils.QUERY_ADDRESS_NOPAGE)
    @ResponseBody
    public RestResponse queryAddressNoPage(@RequestParam String pid,Address address){
        address.setPid(Integer.parseInt(pid));
        List<Address> allList = addressService.queryAddressByParentId(address);
        return GetRest.getSuccess("",allList);
    }
    
    /**
     * 地址启用，禁用
     * @param id 
     * @param status 状态
     * @return
     * @author xiaoky
     */
    @RequestMapping(value = MappingUtils.EDIT_ADDRESS_STATUS)
    @ResponseBody
    public RestResponse editAddressStatus (Integer id, Integer status) {
    	Address address = addressService.getById(id);
    	if (address == null) {
    		return GetRest.getFail("操作失败");
    	}
    	address.setStatus(status);
    	boolean result = addressService.updateById(address);
    	if (result) {
    		return GetRest.getSuccess("操作成功");
    	}
    	return GetRest.getFail("操作成功");
    }
    
    /**
     * 编辑/新增地址页面
     * @param id
     * @return
     * @author xiaoky
     */
    @RequestMapping(value = MappingUtils.SHOW_EDIT_ADDRESS)
    public ModelAndView showEditAddress (Integer id) {
    	ModelAndView view = new ModelAndView(MappingUtils.SHOW_EDIT_ADDRESS);
    	Address address = addressService.getById(id);
    	view.addObject("address", address);
    	return view;
    }
    
    /**
     * 保存地址
     * @param address 地址信息
     * @return 
     * @author xiaoky
     */
    @RequestMapping(value = MappingUtils.DO_EDIT_ADDRESS)
    @ResponseBody
    public RestResponse doEditAddress (Address address) {
    	if (StringUtils.isBlank(address.getName())) {
    		return GetRest.getFail("请输入中文名称");
    	}
    	if (StringUtils.isBlank(address.getLongitude())) {
    		return GetRest.getFail("请输入经度");
    	}
    	if (StringUtils.isBlank(address.getLatitude())) {
    		return GetRest.getFail("请输入纬度");
    	}
    	boolean result = addressService.saveOrUpdate(address);
    	if (result) {
    		return GetRest.getSuccess("保存成功");
    	}
    	return GetRest.getFail("保存失败");
    }
}
